package com.module.boots.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.module.boots.api.constant.ApiConstant;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置
 * @author:溪云阁
 * @date:2020年5月2日
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {

    /**
     * 后台服务接口
     * @author 溪云阁
     * @date 2019年5月22日
     * @return Docket
     */
    @Bean
    public Docket webApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(ApiConstant.SW_TITLE_WEB)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage(ApiConstant.SW_PACKPATH_WEB))
                .paths(PathSelectors.regex(".*/view/.*"))
                .build()
                .apiInfo(new ApiInfoBuilder()
                        // 页面标题
                        .title(ApiConstant.SW_TITLE_WEB)
                        // 创建人
                        .contact(new Contact(ApiConstant.SW_AUTHOR, ApiConstant.SW_EMAIL, ApiConstant.SW_URL))
                        // 版本号
                        .version(ApiConstant.SW_VERSION)
                        // 描述
                        .description(ApiConstant.SW_DESCRIPTION_WEB)
                        .build());
    }

    /**
     * 前台服务接口
     * @author 溪云阁
     * @return Docket
     */
    @Bean
    public Docket appApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(ApiConstant.SW_TITLE_APPLICATION)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage(ApiConstant.SW_PACKPATH_APPLICATION))
                .paths(PathSelectors.regex(".*/application/.*"))
                .build()
                .apiInfo(new ApiInfoBuilder()
                        // 页面标题
                        .title(ApiConstant.SW_TITLE_APPLICATION)
                        // 创建人
                        .contact(new Contact(ApiConstant.SW_AUTHOR, ApiConstant.SW_EMAIL, ApiConstant.SW_URL))
                        // 版本号
                        .version(ApiConstant.SW_VERSION)
                        // 描述
                        .description(ApiConstant.SW_DESCRIPTION_APPLICATION)
                        .build());
    }

    /**
     * 第三方服务接口
     * @author 溪云阁
     * @return Docket
     */
    @Bean
    public Docket externalApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(ApiConstant.SW_TITLE_EXTERNAL)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage(ApiConstant.SW_PACKPATH_EXTERNAL))
                .paths(PathSelectors.regex(".*/external/.*"))
                .build()
                .apiInfo(new ApiInfoBuilder()
                        // 页面标题
                        .title(ApiConstant.SW_TITLE_EXTERNAL)
                        // 创建人
                        .contact(new Contact(ApiConstant.SW_AUTHOR, ApiConstant.SW_EMAIL, ApiConstant.SW_URL))
                        // 版本号
                        .version(ApiConstant.SW_VERSION)
                        // 描述
                        .description(ApiConstant.SW_DESCRIPTION_EXTERNAL)
                        .build());
    }

    /**
     * 所有服务接口
     * @author 溪云阁
     * @return Docket
     */
    @Bean
    public Docket allApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(ApiConstant.SW_TITLE_ALL)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage(ApiConstant.SW_PACKPATH_ALL))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder()
                        // 页面标题
                        .title(ApiConstant.SW_TITLE_ALL)
                        // 创建人
                        .contact(new Contact(ApiConstant.SW_AUTHOR, ApiConstant.SW_EMAIL, ApiConstant.SW_URL))
                        // 版本号
                        .version(ApiConstant.SW_VERSION)
                        // 描述
                        .description(ApiConstant.SW_DESCRIPTION_ALL)
                        .build());
    }

    /**
     * 把swagger的静态页面添加到静态资源中,不然会出现404错误
     * @author 溪云阁
     * @date 2018年12月15日
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}